﻿using MediatR;
using MS.Common.Infr.In;
using MS.Common.Service;

namespace MS.Basic.ReadAPI.Application.Commons.Account
{
    /// <summary>
    /// 账户查询请求处理器
    /// </summary>
    public class AccountQueryRequestHandler:IRequestHandler<AccountQueryRequestModel,APIResult>
    {
        private readonly IUserRepository userRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userRepository"> 用户仓库 </param>
        public AccountQueryRequestHandler(IUserRepository userRepository)
        {
            this.userRepository = userRepository;
        }

        /// <summary>
        /// 处理账户查询请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult> Handle(AccountQueryRequestModel request, CancellationToken cancellationToken)
        {
            APIResult result = new APIResult();
            result.code = (int)MSEnums.登录成功;
            result.message=MSEnums.登录成功.ToString();

            try
            {
                var res = userRepository.GetAll().FirstOrDefault(x => x.UserName == request.name);
                if (res == null)
                {
                    result.code = (int)MSEnums.登录失败;
                    result.message = "登录失败 账号不存在";
                    return Task.FromResult(result);
                }
                if (res.UserPwd != request.pwd)
                {
                    result.code = (int)MSEnums.登录失败;
                    result.message = "登录失败 密码错误";
                    return Task.FromResult(result);
                }
                result.data = res;
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
